package com.lemon.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.admin.bean.User;
import com.lemon.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class tableController {
    /**
     * @param a  不带请求参数或者参数类型不对 400： bad request
     */
    @Autowired
    UserService userService;
    @GetMapping("/basic_table.html")
    public String basic_table(@RequestParam("a") int a){
        //int i=10/0;
        return "/table/basic_table";
    }
    @GetMapping("/dynamic_table.html")
    public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn,
                                Model model){
        //从数据库中查出user表中的用户进行展示
        List<User> list = userService.list();
        model.addAttribute("list",list);
        //构造分页参数
        Page<User> page = new Page<User>(pn,2);
        //调用page进行分页
        Page<User> userPage = userService.page(page, null);

        long total = userPage.getTotal();
        long current = userPage.getCurrent();
        long pages = userPage.getPages();

        model.addAttribute("users",userPage);
        return "table/dynamic_table";
    }
    @GetMapping("/editable_table.html")
    public String edittable_table(){
        return "table/edittable_table";
    }
    @GetMapping("/pricing_table.html")
    public String pricing_table(){
        return "table/pricing_table";
    }
    @GetMapping("/responsive_table.html")
    public String responsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") long id){
        userService.removeById(id);
        return "redirect:/dynamic_table.html";
    }
}
